/*
 * @Author: your name
 * @Date: 2021-12-23 10:31:03
 * @LastEditTime: 2022-01-25 17:08:00
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \custom-app\src\components\parser\caculate.js
 */
const caculateNumber = (item, val) => {
  let endValueArr = []
  item.__config__.formula.value.forEach((cacuChild) => {
    for (let i in val) {
      if (cacuChild === i) {
        if (val[i] == undefined) {
          endValueArr.push(0)
        } else {
          endValueArr.push(parseFloat(val[i]))
        }
      }
    }
  })
  endValueArr.forEach((ele, i) => {
    if (typeof ele === 'number' && isNaN(ele)) {
      endValueArr[i] = 0
    }
  })
  let cacuValue = 0
  if (item.__config__.formula.type === 'product') {
    cacuValue = 1
  }
  endValueArr.forEach((ele, i) => {
    if (item.__config__.formula.type === 'sum') {
      cacuValue += ele
    } else if (item.__config__.formula.type === 'product') {
      cacuValue *= ele
    } else if (item.__config__.formula.type === 'avg') {
      cacuValue += ele
    }
  })
  if (item.__config__.formula.type === 'avg') {
    cacuValue = cacuValue / endValueArr.length
  } else if (item.__config__.formula.type === 'min') {
    cacuValue = Math.min.apply(null, endValueArr)
  } else if (item.__config__.formula.type === 'max') {
    cacuValue = Math.max.apply(null, endValueArr)
  }
  return cacuValue
}

const caculateTime = (item, val) => {
  let endValueArr = []
  item.__config__.formula.value.forEach((cacuChild) => {
    for (let i in val) {
      if (cacuChild === i) {
        endValueArr.push(val[i])
      }
    }
  })
  if (endValueArr[0] === null || endValueArr[1] === null) {
    return
  }
  let format = item.__config__.formula.format
  let unit = item.__config__.formula.unit
  let cacuValue = 0
  let startTime = ''
  let endTime = ''
  if (format === 'HH:mm:ss') {
    startTime = cacuSeconds(endValueArr[0])
    endTime = cacuSeconds(endValueArr[1])
    cacuValue = cacuDayTime(startTime, endTime, unit)
  } else {
    startTime = new Date(endValueArr[0])
    endTime = new Date(endValueArr[1])
    cacuValue = cacuDetailDate(startTime, endTime, unit)
  }
  return cacuValue
}

const getNow = (item, val) => {
  let nowTime = new Date()
  let cacuValue = formatDate(nowTime)
  return cacuValue
}

function formatDate(nowTime) {
  if (!nowTime) {
    return
  }
  let year = nowTime.getFullYear()
  let month = nowTime.getMonth()
  let day = nowTime.getDate()
  let hour = nowTime.getHours()
  let minute = nowTime.getMinutes()
  let second = nowTime.getSeconds()
  if (month < 10) {
    month = '0' + month
  }
  if (day < 10) {
    day = '0' + day
  }
  if (hour < 10) {
    hour = '0' + hour
  }
  if (minute < 10) {
    minute = '0' + minute
  }
  if (second < 10) {
    second = '0' + second
  }
  nowTime =
    year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second
  return nowTime
}

const cacuFutureDate = (item, val) => {
  let endValueArr = []
  item.__config__.formula.value.forEach((cacuChild) => {
    for (let i in val) {
      if (cacuChild === i) {
        if (val[i] == undefined) {
          endValueArr.push(0)
        } else {
          endValueArr.push(val[i])
        }
      }
    }
  })
  let format = item.__config__.formula.format
  let unit = item.__config__.formula.unit
  let addDate = endValueArr[1]
  let date = endValueArr[0]
  let cacuValue = 0
  if (format === 'HH:mm:ss') {
    cacuValue = cacuFutureDay(date, addDate, unit)
  } else {
    if (unit === 'D' || unit === 'h' || unit === 'm' || unit === 's') {
      cacuValue = cacuAddFuture(date, addDate, unit)
    } else if (unit === 'Y' || unit === 'M') {
      cacuValue = cacuFutureMonthOrYear(date, addDate, unit)
    }
  }
  return cacuValue
}

function cacuFutureMonthOrYear(date, addDate, unit) {
  if (!date) {
    return
  }
  let otherStr = date.substr(8)
  let startMonth = parseInt(date.substr(5, 2))
  let startYear = parseInt(date.substr(0, 4))
  let endMonth = 0
  let endYear = 0
  addDate = parseInt(addDate)
  if (unit === 'M') {
    let addMonths = Math.floor((startMonth + addDate) / 12)
    if ((startMonth + addDate) % 12 === 0) {
      addMonths = addMonths - 1
    }
    endYear = startYear + addMonths
    endMonth = (startMonth + addDate) % 12
    if (endMonth === 0) {
      endMonth = 12
    }
  } else if (unit === 'Y') {
    endYear = startYear + addDate
    endMonth = startMonth
  }
  if (endMonth < 10) {
    endMonth = '0' + endMonth
  }
  return endYear + '-' + endMonth + '-' + otherStr
}

function cacuAddFuture(date, addDate, unit) {
  if (!date) {
    return
  }
  var nd = new Date(Date.parse(date.replace(/-/g, '/')))
  nd = nd.valueOf() //转换为毫秒数
  let addTime = 0
  if (unit === 'D') {
    addTime = parseInt(addDate) * 1000 * 3600 * 24
  } else if (unit === 'h') {
    addTime = parseInt(addDate) * 1000 * 3600
  } else if (unit === 'm') {
    addTime = parseInt(addDate) * 1000 * 60
  } else if (unit === 's') {
    addTime = parseInt(addDate) * 1000
  }
  nd = nd + addTime
  nd = new Date(nd)
  var y = nd.getFullYear()
  var m = nd.getMonth() + 1
  var d = nd.getDate()
  var hh = nd.getHours()
  var mm = nd.getMinutes()
  var ss = nd.getSeconds()
  if (m <= 9) m = '0' + m
  if (d <= 9) d = '0' + d
  if (hh <= 9) hh = '0' + hh
  if (mm <= 9) mm = '0' + mm
  if (ss <= 9) ss = '0' + ss
  var date = y + '-' + m + '-' + d + ' ' + hh + ':' + mm + ':' + ss
  return date
}

function cacuFutureDay(date, addDate, unit) {
  if (!date) {
    return
  }
  date = date.substr(date.length - 8, date.length)
  let newTime = cacuSeconds(date)
  addDate = parseInt(addDate)
  let addTime = 0
  if (unit === 'h') {
    addTime = addDate * 3600
  } else if (unit === 'm') {
    addTime = addDate * 60
  } else if (unit === 's') {
    addTime = addDate
  }
  let newDate = newTime + addTime
  let cacuValue = formatDay(newDate)
  return cacuValue
}

function formatDay(value) {
  value = value % 86400
  var seconds = parseInt(value) // 秒
  var minutes = 0 // 分
  var hours = 0 // 小时
  if (seconds > 60) {
    minutes = parseInt(seconds / 60)
    seconds = parseInt(seconds % 60)
    if (minutes > 60) {
      hours = parseInt(minutes / 60)
      minutes = parseInt(minutes % 60)
    }
  }
  if (hours < 10) {
    hours = '0' + hours
  }
  if (minutes < 10) {
    minutes = '0' + minutes
  }
  if (seconds < 10) {
    seconds = '0' + seconds
  }
  let result = '' + hours + ':' + minutes + ':' + seconds
  return result
}

function cacuDayTime(startTime, endTime, unit) {
  let ms = 0
  if (unit === 'h') {
    ms = Math.abs((endTime - startTime) / 3600)
  } else if (unit === 'm') {
    ms = Math.abs((endTime - startTime) / 60)
  } else if (unit === 's') {
    ms = Math.abs(endTime - startTime)
  }
  return ms
}

function cacuSeconds(item) {
  let staTime = ''
  if (item) {
    staTime = item.substring(item.length - 8, item.length)
    staTime = item.split(':')
  }
  let newTime =
    parseFloat(staTime[0] * 60 * 60) +
    parseFloat(staTime[1] * 60) +
    parseFloat(staTime[2])
  return newTime
}

function cacuDetailDate(startTime, endTime, unit) {
  function getDays(mouth, year) {
    let days = 30
    if (mouth === 2) {
      days = year % 4 === 0 ? 29 : 28
    } else if (
      mouth === 1 ||
      mouth === 3 ||
      mouth === 5 ||
      mouth === 7 ||
      mouth === 8 ||
      mouth === 10 ||
      mouth === 12
    ) {
      days = 31
    }
    return days
  }
  const start = new Date(startTime)
  const end = new Date(endTime)
  const diffValue = Math.abs(end - start)
  const startYear = start.getFullYear()
  const endYear = end.getFullYear()
  const startMouth = start.getMonth() + 1
  const endMouth = end.getMonth() + 1
  const startDay = start.getDate()
  const endDay = end.getDate()
  const startHours = start.getHours()
  const endHours = end.getHours()
  const startMinutes = start.getMinutes()
  const endMinutes = end.getMinutes()
  const startSeconds = start.getSeconds()
  const endSeconds = end.getSeconds()
  if (unit === 'Y' || unit === 'M') {
    const diffYear = Math.abs(endYear - startYear)
    const startDays = getDays(startMouth, startYear)
    const endDays = getDays(endMouth, endYear)
    const diffStartMouth =
      (startDays -
        (startDay +
          (startHours * 60 + startMinutes + startSeconds / 60) / 60 / 24 -
          1)) /
      startDays
    const diffEndMouth =
      (endDay + (endHours * 60 + endMinutes + endSeconds / 60) / 60 / 24 - 1) /
      endDays
    const diffMouth =
      diffStartMouth +
      diffEndMouth +
      (12 - startMouth - 1) +
      endMouth +
      (diffYear - 1) * 12
    if (unit === 'Y') {
      return parseFloat((diffMouth / 12) * 100) / 100
    } else {
      return parseFloat(diffMouth)
    }
  } else if (unit === 'D') {
    const d = parseFloat(diffValue / 1000 / 60 / 60 / 24)
    return d
  } else if (unit === 'h') {
    const h = parseFloat(diffValue / 1000 / 60 / 60)
    return h
  } else if (unit === 'm') {
    const m = parseFloat(diffValue / 1000 / 60)
    return m
  } else if (unit === 's') {
    const s = parseFloat(diffValue / 1000)
    return s
  }
}
export { caculateNumber, caculateTime, getNow, cacuFutureDate }
